package com.maxjay.component.metadata.core.parser;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.maxjay.common.util.json.JsonParser;
import com.maxjay.component.metadata.constant.FieldType;
import com.maxjay.component.metadata.constant.FormatterType;
import com.maxjay.component.metadata.core.formatter.CodeFieldFormatter;
import com.maxjay.component.metadata.core.formatter.DateFieldFormatter;
import com.maxjay.component.metadata.core.formatter.FieldFormatter;
import com.maxjay.component.metadata.core.generator.FieldValueGenerator;
import com.maxjay.component.metadata.model.DefaultEntity;
import com.maxjay.component.metadata.model.DefaultEntityField;
import com.maxjay.component.metadata.model.Entity;
import com.maxjay.component.metadata.model.EntityConfig;
import com.maxjay.system.constant.YesNoConstant;
import com.maxjay.system.core.Application;
import com.maxjay.system.service.BaseServiceJdbc;

public class DatabaseEntityParser extends BaseServiceJdbc implements EntityParser {

	@Override
	public Entity parseEntity(String entityId) {
		String qryEntitySql = "select * from T_MD_ENTITY t where t.OBJ_ID = ?";
		Map<String, Object> entityMap = this.getJdbcTemplate().queryForMap(qryEntitySql, entityId);
		DefaultEntity entity = new DefaultEntity(entityId, (String)entityMap.get("NAME"), (String)entityMap.get("NAME_CN"));
		entity.setTabName((String)entityMap.get("NAME"));
		// 解析实体配置信息
		EntityConfig entityConfig = new EntityConfig();
		String formCreatePageLocation = (String)entityMap.get("CREATE_PAGE_LOCATION");
		String formUpdatePageLocation = (String)entityMap.get("UPDATE_PAGE_LOCATION");
		String formPluginScript = (String)entityMap.get("FORM_PLUGIN_SCRIPT");
		EntityConfig.ViewConfig viewConfig = entityConfig.new ViewConfig(formCreatePageLocation, formUpdatePageLocation);
		viewConfig.setEntityPluginScript(formPluginScript);
		entityConfig.setViewConfig(viewConfig);
		entity.setEntityConfig(entityConfig);
		
		this.parseEntityField(entity);
		return entity;
	}
	
	/**
	 * 解析实体对象属性
	 * 
	 * @param entity
	 */
	private void parseEntityField(Entity entity) {
		String qryFieldSql = "select * from T_MD_ENTITY_FIELD t where t.ENTITY_ID = ? order by t.ORDER_NO asc";
		List<Map<String, Object>> fieldMapList = this.getJdbcTemplate().queryForList(qryFieldSql, entity.getId());
		for (Map<String, Object> fieldMap : fieldMapList) {
			DefaultEntityField field = new DefaultEntityField((String)fieldMap.get("OBJ_ID"), (String)fieldMap.get("NAME"), 
					(String)fieldMap.get("NAME_CN"));
//			FieldFormatter formatter = EntityContextHolder.getContext().getFormatterFactory().getFieldFormatter((String)fieldMap.get("FORMATTER_ID"));
			field.setFieldType(FieldType.valueOf((String)fieldMap.get("FIELD_TYPE")));
			String formatterType = (String)fieldMap.get("FORMATTER_TYPE");
			if (StringUtils.isNotBlank(formatterType)) {
				String formatterConf = (String)fieldMap.get("FORMATTER_CONF"); 
				Map<String, String> formatterConfMap = JsonParser.parseJsonStr(formatterConf);
				FieldFormatter fieldFormatter = null;
				switch (FormatterType.valueOf(formatterType)) {
				case CODE_FORMATTER:
					fieldFormatter = new CodeFieldFormatter(formatterConfMap.get("codeTab"));
					break;
				case DATE_FORMATTER:
					fieldFormatter = new DateFieldFormatter(formatterConfMap.get("displayFormat"), formatterConfMap.get("storeFormat"));
					break;
				default:
					break;
				}
				field.setFormatter(fieldFormatter);
			}
			String fieldValGenerator = (String)fieldMap.get("GENERATOR");
			if (StringUtils.isNotBlank(fieldValGenerator)) {
				//TODO 如果spring中无此bean则会报错。。。，需捕获异常
				field.setValueGenerator(Application.getBeanContext().getBean(fieldValGenerator, FieldValueGenerator.class));
				try {
					field.setValueGenerator((FieldValueGenerator)Class.forName(fieldValGenerator).newInstance());
				} catch (Exception e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			if (YesNoConstant.YES.equals(fieldMap.get("IS_KEY"))) {
				field.setKey(true);
			}
			if (YesNoConstant.NO.equals(fieldMap.get("IS_DISPLAY"))) {
				field.setDisplay(false);
			}
			if (YesNoConstant.NO.equals(fieldMap.get("IS_NULLABLE"))) {
				field.setNullable(false);
			}
			entity.addEntityField(field);
		}
	}

}
